package com.rulai.generator.plugin.support;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rulai.generator.framework.util.PropertiesFileUtil;
import com.rulai.generator.plugin.GeneratorRepository;
import com.rulai.generator.plugin.SkeletonProject;
import com.rulai.generator.plugin.model.Table2Model;
import com.rulai.generator.plugin.repository.JdbcGeneratorRepository;
import com.rulai.generator.plugin.terminator.RepositoryMavenProject;
import com.rulai.generator.plugin.terminator.ServiceFacadeMavenProject;
import com.rulai.generator.plugin.terminator.ServiceImplMavenProject;

/**
 * 从jdbc 构建 spring mvc模型代码工程生成工具 <功能详细描述>
 * 
 * @since [产品/模块版本]
 * @author leiyang@qding.me
 * @version v1.0 Date:2018年5月10日 下午2:59:55
 */
public class JDBC2SpringMVCBuilder {

    private static final Logger LOG = LoggerFactory.getLogger(JDBC2SpringMVCBuilder.class);

    private PropertiesFileUtil propertiesFileUtil;
    
    public JDBC2SpringMVCBuilder(String  propertie) {
        this.propertiesFileUtil = PropertiesFileUtil.getInstance(propertie);
    }

    public JDBC2SpringMVCBuilder(PropertiesFileUtil propertiesFileUtil) {
        this.propertiesFileUtil = propertiesFileUtil;
    }

    public void begin() {

        String jdbcDriver = propertiesFileUtil.get("generator.jdbc.driver");
        String jdbcUrl = propertiesFileUtil.get("generator.jdbc.url");
        String jdbcUsername = propertiesFileUtil.get("generator.jdbc.username");
        String jdbcPassword = propertiesFileUtil.get("generator.jdbc.password");
        String database = propertiesFileUtil.get("generator.jdbc.database");
        String tablePrefix = propertiesFileUtil.get("generator.table.tablePrefix");
        String module = propertiesFileUtil.get("generator.module");
        String packageName = propertiesFileUtil.get("generator.packageName");
        String projectPath = propertiesFileUtil.get("generator.path");
        GeneratorRepository generatorRepository = new JdbcGeneratorRepository(jdbcDriver, jdbcUrl, jdbcUsername,
                jdbcPassword, database, tablePrefix);
        List<Table2Model> tables = generatorRepository.getTableModels();

        try {
            //生成dao工程
            SkeletonProject skeletonProject = new RepositoryMavenProject(projectPath, true, tables, packageName,
                    module);
            skeletonProject.build();
            
            //生成facade工程
            SkeletonProject serviceFacadeMavenProject = new ServiceFacadeMavenProject(projectPath, true, tables,
                    packageName, module);
            serviceFacadeMavenProject.build();
            
            //生成服务实现工程
            SkeletonProject serviceImplMavenProject = new ServiceImplMavenProject(projectPath, true, tables,
                    packageName, module);
            serviceImplMavenProject.build();
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }

    }

}
